// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/audio_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/audio"
	audioModel "gin-sns/app/model/module/audio"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var AudioService = newAudioService()

func newAudioService() *audioService {
	return &audioService{}
}

type audioService struct {
}

//根据主键查询数据
func (s *audioService) SelectRecordById(id int64) (*audioModel.Entity, error) {
	return audio.AudioDao.SelectById(id)
}

//
func (s *audioService) SelectRecordsIn(idarr []int64) ([]audioModel.Entity, error) {

	list, err := audio.AudioDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *audioService) DeleteRecordById(id int64) bool {
	return audio.AudioDao.DeleteById(id)
}

//批量删除数据记录
func (s *audioService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := audio.AudioDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *audioService) AddSave(req *audioModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity audioModel.Entity

	//entity.Id = req.Id
	entity.BlogId = req.BlogId
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.Mp3Url = req.Mp3Url
	entity.Length = req.Length
	entity.Ptime = req.Ptime
	entity.CountPlay = req.CountPlay
	entity.CountComment = req.CountComment
	entity.CountLike = req.CountLike
	entity.CountFavor = req.CountFavor
	entity.CountReward = req.CountReward
	entity.Status = req.Status
	entity.LastCommentTime = req.LastCommentTime
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := audio.AudioDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *audioService) EditSave(req *audioModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &audioModel.Entity{Id: req.Id}
	ok, err := audio.AudioDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.BlogId = req.BlogId
	entity.UserId = req.UserId
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Logo = req.Logo
	entity.Mp3Url = req.Mp3Url
	entity.Length = req.Length
	entity.Ptime = req.Ptime
	entity.CountPlay = req.CountPlay
	entity.CountComment = req.CountComment
	entity.CountLike = req.CountLike
	entity.CountFavor = req.CountFavor
	entity.CountReward = req.CountReward
	entity.Status = req.Status
	entity.LastCommentTime = req.LastCommentTime
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return audio.AudioDao.Update(entity)
}

//根据条件查询数据
func (s *audioService) SelectListAll(params *audioModel.SelectPageReq) ([]audioModel.Entity, error) {
	return audio.AudioDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *audioService) SelectListByPage(params *audioModel.SelectPageReq) ([]audioModel.Entity, *page.Paging, error) {
	return audio.AudioDao.SelectListByPage(params)
}

// 导出excel
func (s *audioService) Export(param *audioModel.SelectPageReq) (string, error) {
	head := []string{"", "博客id", "用户id", "标题", "描述", "封面logo", "MP3 URL", "文件长度", "播放时间", "播放次数", "评论次数", "赞同次数", "收藏次数", "打赏次数", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "更新时间", "最后评论时间"}
	col := []string{"id", "blog_id", "user_id", "title", "description", "logo", "mp3_url", "length", "ptime", "count_play", "count_comment", "count_like", "count_favor", "count_reward", "status", "create_time", "update_time", "last_comment_time"}
	return audio.AudioDao.SelectListExport(param, head, col)
}
